<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>我是一阵风直播</title>
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
    <style>
        #aui-header{position: fixed;}
    </style>
</head>
<body>
  <header class="aui-bar aui-bar-nav" id="aui-header">
      <a class="aui-pull-left aui-btn" tapmode onclick="back()">
          <span class="aui-iconfont aui-icon-left"></span>
      </a>
      <div class="aui-title">扫码</div>
      <a class="aui-pull-right aui-btn" tapmode onclick="fnOpenLamp()">
          <span class="aui-iconfont aui-icon-display"></span>
      </a>
  </header>
</body>
<script type="text/javascript" src="../script/api.js" ></script>
<script type="text/javascript" src="../script/common.js" ></script>
<script>
    var FNScanner;
    apiready = function() {
        //应用全局FNScanner模块
        FNScanner = api.require('FNScanner');
        fixStatusBar();

        //监听应用回到前台
        api.addEventListener({
            name: 'resume'
        }, function(ret, err) {
            FNScanner.onResume();
        });

        //监听应用回到后台
        api.addEventListener({
            name: 'pause'
        }, function(ret, err) {
            FNScanner.onPause();
        });
        // 打开后置摄像头扫码
        fnOpenFNScanner();
    }
    //扫码
    function fnOpenFNScanner() {
        // 打开指定自定义模块
        FNScanner.openView({
            //autorotation: true,
            fixedOn: api.frameName,
            rect: {
                x: 0,
                y: headerHeight,
                w: api.frameWidth,
                h: api.frameHeight
            }
        }, function(ret, err) {
            if (ret) {
                if (ret.eventType == 'success' && ret.content != '') {
                    // 关闭扫码模块
                    FNScanner.closeView();
                    // 扫码成功执行函数操作传值
                    console.log(ret.content);
                    api.openFrame({
                        name: 'home_frm',
                        url: './home_frm.html',
                        pageParam:{
                          rtmp_url:ret.content,
                        }
                    });
                    return;
                }
                if (ret.eventType == 'cameraError') {
                    api.toast({
                        msg: '请开启app访问手机摄像头权限',
                        duration: 2000,
                        location: 'bottom'
                    });
                    setTimeout(function() {
                        // 关闭扫码器  关闭扫码界面
                        back();
                    }, 300);
                    return;
                }
                if (ret.eventType == 'albumError') {
                    api.toast({
                        msg: '请开启app访问手机相册权限',
                        duration: 2000,
                        location: 'bottom'
                    });
                    setTimeout(function() {
                        back();
                    }, 300);
                    return;
                }
                if (ret.eventType == 'fail') {
                    api.toast({
                        msg: '扫码失败,请从新扫码',
                        duration: 2000,
                        location: 'bottom'
                    });
                    setTimeout(function() {
                        back();
                    }, 300);
                }
            } else {
                api.toast({
                    msg: '扫码错误请稍后再试',
                    duration: 2000,
                    location: 'bottom'
                });
                setTimeout(function() {
                    back();
                }, 300);
            }
        });
        setTimeout(function() {
            // 打开扫码动画frm页面
            api.openFrame({
                name: 'fnscanner_add_frm',
                url: './fnscanner_add_frm.html',
                rect: {
                    marginTop: headerHeight,
                    marginLeft: 0,
                    marginRight: 0,
                    marginBottom: 0
                },
                bgColor: 'rgba(0,0,0,0)',
            });
        }, 300);
    }
    var isSOff = true;
    // 打开闪光灯
    function fnOpenLamp() {
        if (isSOff) {
            FNScanner.switchLight({
                status: 'on'
            });
            isSOff = false;
        } else {
            FNScanner.switchLight({
                status: 'off'
            });
            isSOff = true;
        }
    }
    //返回
    function back() {
        // 关闭win页面
        api.closeWin();
        // 关闭扫码模块
        FNScanner.closeView();
    }
</script>

</html>
